package com.example.softwareconfigurationcoursedesign;

import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet("/AddUserServlet")
public class AddUserServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        // 获取前端传递的参数
        String name = request.getParameter("userName");
        String phoneNumber = request.getParameter("phoneNumber");
        String password = request.getParameter("password");

        // 输出用户信息到控制台
        System.out.println("添加用户信息：");
        System.out.println("姓名：" + name);
        System.out.println("手机号码：" + phoneNumber);
        System.out.println("密码：" + password);

        // 在这里执行添加会员的逻辑
        AdminServlet adminServlet = new AdminServlet();
        User user = new User(0, name, phoneNumber, password);
        adminServlet.addUserToDatabase(user);
        System.out.println("添加会员");

        // 返回成功响应
        response.getWriter().write("添加成功");
    }
}
